<template>
	<el-dialog
    title="用户管理"
    :visible.sync="addyh"
    width="50%"
    :before-close="handleClose">
    <el-form :model="userInfo" :rules="rules" ref="userInfo">
        <table>
            <tr>
                <td>用户名</td>
                <td>
                    <el-form-item prop="username">
                        <el-input v-model="userInfo.username"></el-input>
                    </el-form-item>
                </td>
            </tr>
            <tr>
                <td>密码</td>
                <td>
                    <el-form-item prop="password">
                        <el-input v-model="userInfo.password"></el-input>
                    </el-form-item>
                </td>
            </tr>
            <tr>
                <td>角色</td>
                <td>
                    <el-checkbox-group v-model="userInfo.roles">
                        <el-checkbox v-for="(role,key) in allRoles" :key="key" :label="role._id">
                            {{role.roleName}}
                        </el-checkbox>
                    </el-checkbox-group>
                </td>
            </tr>
        </table>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="addNewUser" v-if="isAddNew">确 定</el-button>
        <el-button type="primary" @click="updateUserInfo" v-else>更新</el-button>
    </span>
    </el-dialog>
</template>

<script>
export default {
    props: ['userInfo','isAddNew'],
    data(){
        return {
            addyh: false,
            rules: {
                username: [
                    {required: true,message:'请输入账号名称',tigger:'flur'},
                    {min:2,max:20,message:'用户名不能少于2个字符'}
                ],
                password: [
                    {required: true,message:'请输入密码',tigger:'flur'},
                    {min:5,message:'密码不能少于5个字符'}
                ]
            }
        }
    },
    computed: {
        allRoles(){
            return this.$store.getters.allRoles;
        }
    },
    methods: {
        handleClose(){
            this.addyh = false;
            this.$parent.showAddNew = false;
        },
        addNewUser(){
            this.$refs['userInfo'].validate((valid) => {
            if (valid) {
                this.$http.post(this.$api.addNewUser,this.userInfo)
                .then((res)=>{
                    this.$message({
                        message: res.message,
                        type: res.success?'success':'danger'
                    });
                })
                this.handleClose();
                this.$store.dispatch('loadAllUser',{api:this.$api.allUsers,pageNum:1,pageSize:10})

            } else {
                console.log('error submit!!');
                return false;
            }
            });
        },
        updateUserInfo(){
            // 更新账号信息
            this.$refs['userInfo'].validate((valid) => {
            if (valid) {
                this.$http.post(this.$api.updateUserInfo,this.userInfo)
                .then((res)=>{
                    this.$message({
                        message: res.message,
                        type: res.success?'success':'danger'
                    });
                })
                this.handleClose();
                this.$store.dispatch('loadAllUser',{api:this.$api.allUsers,pageNum:1,pageSize:10})
            } else {
                console.log('error submit!!');
                return false;
            }
            });
        }
    }
}
</script>

<style>
</style>